﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDImageFilterUtil.h"

CAImage* GDImageFilterUtil::modefyImage(CAImage *sender, int filterId, float bright, float contrast, float saturation, int hue, int cbr, int cbg, int cbb)
{
	if (sender == NULL || filterId <= 0){
		return sender;
	}
	return GDImageFilterUtil::BCSHFilter(sender, bright, contrast, saturation, hue, cbr, cbg, cbb);
}

CAImage* GDImageFilterUtil::ImageFilter1(CAImage *image, int filterId)
{
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	int r = 0, g = 0, b = 0, avg = 0;

	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++)
		{
			r = GDImageFilterUtil::getRComponent(_image, width, height, y, x, rgbaType);
			g = GDImageFilterUtil::getGComponent(_image, width, height, y, x, rgbaType);
			b = GDImageFilterUtil::getBComponent(_image, width, height, y, x, rgbaType);
			avg = r*0.39 + g*0.5 + b*0.11;

			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, avg, avg, avg, rgbaType);
		}
	}

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::ImageFilter2(CAImage *image, int filterId)
{
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	int r = 0, g = 0, b = 0, avg = 0;

	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++)
		{
			r = GDImageFilterUtil::getRComponent(_image, width, height, y, x, rgbaType);
			g = GDImageFilterUtil::getGComponent(_image, width, height, y, x, rgbaType);
			b = GDImageFilterUtil::getBComponent(_image, width, height, y, x, rgbaType);
			avg = (r + g + b) / 3;
			avg = avg >= ThreshHold ? 255 : 0;

			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, avg, avg, avg, rgbaType);
		}
	}

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::ImageFilter3(CAImage *image, int filterId)
{
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	int r = 0, g = 0, b = 0;

	for (int x = 0; x < width-1; x++) {
		for (int y = 0; y < height-1; y++)
		{
			for (int row = 0; row <= 1; row++){
				for (int col = 0; col <= 1; col++){
					if (row == 0 && col == 0){
						continue;
					}
					r += GDImageFilterUtil::getRComponent(_image, width, height, y+row, x+col, rgbaType);
					g += GDImageFilterUtil::getGComponent(_image, width, height, y + row, x + col, rgbaType);
					b += GDImageFilterUtil::getBComponent(_image, width, height, y + row, x + col, rgbaType);
				}
			}
			r = r / 3;
			g = g / 3;
			b = b / 3;

			r = GDImageFilterUtil::getRComponent(_image, width, height, y, x, rgbaType) + (GDImageFilterUtil::getRComponent(_image, width, height, y, x, rgbaType) - r)*0.3;
			g = GDImageFilterUtil::getGComponent(_image, width, height, y, x, rgbaType) + (GDImageFilterUtil::getGComponent(_image, width, height, y, x, rgbaType) - g)*0.3;
			b = GDImageFilterUtil::getBComponent(_image, width, height, y, x, rgbaType) + (GDImageFilterUtil::getBComponent(_image, width, height, y, x, rgbaType) - b)*0.3;
			
			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, r, g, b, rgbaType);
		}
	}

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::HslModifyFilter(CAImage *image, float hueFactor, float s, float l)
{
	unsigned char* sourceData = image->getData(); // 原始图片数据
	// 目标图片数据
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	// 滤镜算法代码 
	hueFactor = MAX(0, MIN(359, hueFactor));
	
	int r, g, b;
	HslColor hsl(hueFactor, s, l);

	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			r = GDImageFilterUtil::getRComponent(sourceData, width, height, y, x, rgbaType);
			g = GDImageFilterUtil::getGComponent(sourceData, width, height, y, x, rgbaType);
			b = GDImageFilterUtil::getBComponent(sourceData, width, height, y, x, rgbaType);

			HslColor::RgbToHsl(r, g, b, hsl);
			hsl.h += hueFactor;
			int color = HslColor::HslToRgb(hsl);
			GDImageFilterUtil::setPixelColor(_image, width, height, y, x, color, rgbaType);
		}
	}
	// 滤镜算法代码 结束

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}


CAImage* GDImageFilterUtil::YCBCrLinearFilter(CAImage *image, float minCb, float maxCb, float minCr, float maxCr)
{
	Range *inY = new Range(0.0f, 1.0f); //(0.0f, 1.0f);
	Range *inCb = new Range(minCb, maxCb); //(-0.5f, 0.5f);
	Range *inCr = new Range(minCr, maxCr); //(-0.5f, 0.5f);
	Range *outY = new Range(0.0f, 1.0f); //(0.0f, 1.0f);
	Range *outCb = new Range(-0.5f, 0.5f); //(-0.5f, 0.5f);
	Range *outCr = new Range(-0.5f, 0.5f); //(-0.5f, 0.5f);


	unsigned char* sourceData = image->getData(); // 原始图片数据
	// 目标图片数据
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	// 滤镜算法代码 
	Color rgb;
	YCbCr ycbcr;

	float ky = 0, by = 0;
	float kcb = 0, bcb = 0;
	float kcr = 0, bcr = 0;

	// Y line parameters
	if (inY->Max != inY->Min)
	{
		ky = (outY->Max - outY->Min) / (inY->Max - inY->Min);
		by = outY->Min - ky * inY->Min;
	}
	// Cb line parameters
	if (inCb->Max != inCb->Min)
	{
		kcb = (outCb->Max - outCb->Min) / (inCb->Max - inCb->Min);
		bcb = outCb->Min - kcb * inCb->Min;
	}
	// Cr line parameters
	if (inCr->Max != inCr->Min)
	{
		kcr = (outCr->Max - outCr->Min) / (inCr->Max - inCr->Min);
		bcr = outCr->Min - kcr * inCr->Min;
	}

	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			rgb.R = GDImageFilterUtil::getRComponent(sourceData, width, height, y, x, rgbaType);
			rgb.G = GDImageFilterUtil::getGComponent(sourceData, width, height, y, x, rgbaType);
			rgb.B = GDImageFilterUtil::getBComponent(sourceData, width, height, y, x, rgbaType);

			// convert to YCbCr
			ycbcr = YCbCr::FromRGB(rgb, ycbcr);

			// correct Y
			if (ycbcr.Y >= inY->Max)
				ycbcr.Y = outY->Max;
			else if (ycbcr.Y <= inY->Min)
				ycbcr.Y = outY->Min;
			else
				ycbcr.Y = ky * ycbcr.Y + by;

			// correct Cb
			if (ycbcr.Cb >= inCb->Max)
				ycbcr.Cb = outCb->Max;
			else if (ycbcr.Cb <= inCb->Min)
				ycbcr.Cb = outCb->Min;
			else
				ycbcr.Cb = kcb * ycbcr.Cb + bcb;

			// correct Cr
			if (ycbcr.Cr >= inCr->Max)
				ycbcr.Cr = outCr->Max;
			else if (ycbcr.Cr <= inCr->Min)
				ycbcr.Cr = outCr->Min;
			else
				ycbcr.Cr = kcr * ycbcr.Cr + bcr;

			// convert back to RGB
			rgb = YCbCr::ToRGB(ycbcr, rgb);

			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, rgb.R, rgb.G, rgb.B, rgbaType);
		}
	}
	// 滤镜算法代码 结束

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::SpodogramFilter(CAImage *image)
{
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	int r = 0, g = 0, b = 0, avg = 0;

	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++)
		{
			r = GDImageFilterUtil::getRComponent(_image, width, height, y, x, rgbaType);
			g = GDImageFilterUtil::getGComponent(_image, width, height, y, x, rgbaType);
			b = GDImageFilterUtil::getBComponent(_image, width, height, y, x, rgbaType);
			avg = r*0.39 + g*0.5 + b*0.11;

			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, avg, avg, avg, rgbaType);
		}
	}

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::BrightContrastFilter(CAImage *image, float BrightnessFactor, float ContrastFactor)
{
	unsigned char* sourceData = image->getData(); // 原始图片数据
	// 目标图片数据
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	// 滤镜算法代码 
	int r, g, b;
	// Convert to integer factors
	int bfi = (int)(BrightnessFactor * 255);
	float cf = 1 + ContrastFactor;
	cf *= cf;
	int cfi = (int)(cf * 32768) + 1;
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			r = GDImageFilterUtil::getRComponent(sourceData, width, height, y, x, rgbaType);
			g = GDImageFilterUtil::getGComponent(sourceData, width, height, y, x, rgbaType);
			b = GDImageFilterUtil::getBComponent(sourceData, width, height, y, x, rgbaType);
			// Modify brightness (addition)
			if (bfi != 0)
			{
				// Add brightness
				int ri = r + bfi;
				int gi = g + bfi;
				int bi = b + bfi;
				// Clamp to byte boundaries
				r = (unsigned char)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
				g = (unsigned char)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
				b = (unsigned char)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
			}
			// Modifiy contrast (multiplication)
			if (cfi != 32769)
			{
				// Transform to range [-128, 127]
				int ri = r - 128;
				int gi = g - 128;
				int bi = b - 128;

				// Multiply contrast factor
				ri = (ri * cfi) >> 15;
				gi = (gi * cfi) >> 15;
				bi = (bi * cfi) >> 15;

				// Transform back to range [0, 255]
				ri = ri + 128;
				gi = gi + 128;
				bi = bi + 128;

				// Clamp to byte boundaries
				r = (unsigned char)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
				g = (unsigned char)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
				b = (unsigned char)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
			}
			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, r, g, b, rgbaType);
		}
	}
	// 滤镜算法代码 结束

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::SaturationFilter(CAImage *image, float Saturation)
{
	unsigned char* sourceData = image->getData(); // 原始图片数据
	// 目标图片数据
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	// 滤镜算法代码 
	float saturation = Saturation + 1;
	float negosaturation = 1 - saturation;
	int r, g, b;
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			r = GDImageFilterUtil::getRComponent(sourceData, width, height, y, x, rgbaType);
			g = GDImageFilterUtil::getGComponent(sourceData, width, height, y, x, rgbaType);
			b = GDImageFilterUtil::getBComponent(sourceData, width, height, y, x, rgbaType);

			float nego1 = negosaturation * 0.2126;
			float ngeo2 = nego1 + saturation;
			float ngeo3 = negosaturation * 0.7152;
			float nego4 = ngeo3 + saturation;
			float nego5 = negosaturation * 0.0722;
			float nego6 = nego5 + saturation;
			float rr = ((r * ngeo2) + (g * ngeo3)) + (b * nego5);
			float gg = ((r * nego1) + (g * nego4)) + (b * nego5);
			float bb = ((r * nego1) + (g * ngeo3)) + (b * nego6);
			r = SAFECOLOR(rr);
			g = SAFECOLOR(gg);
			b = SAFECOLOR(bb);
			GDImageFilterUtil::setRGBComponent(_image, width, height, y, x, r, g, b, rgbaType);
		}
	}
	// 滤镜算法代码 结束

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

CAImage* GDImageFilterUtil::BCSHFilter(CAImage *image, float bright, float contrast, float Saturation, int hue,int cbr,int cbg,int cbb)
{
	//unsigned char* sourceData = image->getData(); // 原始图片数据
	// 目标图片数据
	unsigned char* _image = static_cast<unsigned char*>(malloc(image->getDataLenght() * sizeof(unsigned char)));
	memcpy(_image, image->getData(), image->getDataLenght());

	CAImage::PixelFormat pixelFormat = image->getPixelFormat();

	unsigned int height = image->getPixelsHigh();
	unsigned int width = image->getPixelsWide();
	int rgbaType = 3;
	int ThreshHold = 128;

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{
		rgbaType = 4;
	}

	// 滤镜算法代码 
	int r, g, b, ri, gi, bi;
	// Convert to integer factors
	int bfi = (int)(bright * 255);
	float cf = 1 + contrast;
	cf *= cf;
	int cfi = (int)(cf * 32768) + 1;

	float saturation = Saturation + 1;
	float negosaturation = 1 - saturation;

	hue = MAX(0, MIN(359, hue));

	
	HslColor hsl(hue, 0, 0);
	int curIndex = 0, color=0; //当前像素索引
	float nego1, ngeo2, ngeo3, nego4, nego5, nego6;

	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			curIndex = (y * width + x) * rgbaType;
			r = _image[curIndex];
			g = _image[curIndex+1];
			b = _image[curIndex+2];
			
			if (bright != 0 || contrast != 0){ // 处理亮度对比度
				// Modify brightness (addition)
				if (bfi != 0)
				{
					// Add brightness
					ri = r + bfi;
					gi = g + bfi;
					bi = b + bfi;
					// Clamp to byte boundaries
					r = (unsigned char)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
					g = (unsigned char)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
					b = (unsigned char)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
				}
				// Modifiy contrast (multiplication)
				if (cfi != 32769)
				{
					// Transform to range [-128, 127]
					ri = r - 128;
					gi = g - 128;
					bi = b - 128;

					// Multiply contrast factor
					ri = (ri * cfi) >> 15;
					gi = (gi * cfi) >> 15;
					bi = (bi * cfi) >> 15;

					// Transform back to range [0, 255]
					ri = ri + 128;
					gi = gi + 128;
					bi = bi + 128;

					// Clamp to byte boundaries
					r = SAFECOLOR(ri);
					g = SAFECOLOR(gi);
					b = SAFECOLOR(bi);
				}
			}

			if (Saturation != 0){ // 处理饱和度
				nego1 = negosaturation * 0.2126;
				ngeo2 = nego1 + saturation;
				ngeo3 = negosaturation * 0.7152;
				nego4 = ngeo3 + saturation;
				nego5 = negosaturation * 0.0722;
				nego6 = nego5 + saturation;
				ri = ((r * ngeo2) + (g * ngeo3)) + (b * nego5);
				gi = ((r * nego1) + (g * nego4)) + (b * nego5);
				bi = ((r * nego1) + (g * ngeo3)) + (b * nego6);
				r = SAFECOLOR(ri);
				g = SAFECOLOR(gi);
				b = SAFECOLOR(bi);
			}

			if (hue != 0){
				HslColor::RgbToHsl(r, g, b, hsl);
				hsl.h += hue;
				color = HslColor::HslToRgb(hsl);
				r = (0xFF0000 & color) >> 16;
				g = (0x00FF00 & color) >> 8;
				b = 0x0000FF & color;
				r = SAFECOLOR(ri);
				g = SAFECOLOR(gi);
				b = SAFECOLOR(bi);
			}

			// 处理色彩平衡
			r += cbr;
			g += cbg;
			b += cbb;

			_image[curIndex] = SAFECOLOR(r);
			_image[curIndex+1] = SAFECOLOR(g);
			_image[curIndex+2] = SAFECOLOR(b);
		}
	}
	// 滤镜算法代码 结束

	CAImage* m_pBlackEffect = new CAImage();
	m_pBlackEffect->initWithRawData(_image, image->getPixelFormat(), image->getContentSize().width, image->getContentSize().height);
	m_pBlackEffect->autorelease();
	delete[] _image;

	return m_pBlackEffect;
}

unsigned char GDImageFilterUtil::getRComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, int rgbaType)
{
	return _image[(row * pixelsWide + col) * rgbaType + 0];
}

unsigned char GDImageFilterUtil::getGComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, int rgbaType)
{
	return _image[(row * pixelsWide + col) * rgbaType + 1];
}

unsigned char GDImageFilterUtil::getBComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, int rgbaType)
{
	return _image[(row * pixelsWide + col) * rgbaType + 2];
}

unsigned char GDImageFilterUtil::getAComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col)
{
	return _image[(row * pixelsWide + col) * 4 + 3];
}

void GDImageFilterUtil::setRComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, unsigned char value, int rgbaType)
{
	_image[(row * pixelsWide + col) * rgbaType + 0] = SAFECOLOR(value);
}

void GDImageFilterUtil::setGComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, unsigned char value, int rgbaType)
{
	_image[(row * pixelsWide + col) * rgbaType + 1] = SAFECOLOR(value);
}

void GDImageFilterUtil::setBComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, unsigned char value, int rgbaType)
{
	_image[(row * pixelsWide + col) * rgbaType + 2] = SAFECOLOR(value);
}

void GDImageFilterUtil::setAComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, unsigned char value)
{
	_image[(row * pixelsWide + col) * 4 + 3] = SAFECOLOR(value);
}

void GDImageFilterUtil::setPixelColor(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, int color, int rgbaType)
{
	unsigned char r = (0xFF0000 & color) >> 16;
	unsigned char g = (0x00FF00 & color) >> 8;
	unsigned char b = 0x0000FF & color;
	_image[(row * pixelsWide + col) * rgbaType + 0] = SAFECOLOR(r);
	_image[(row * pixelsWide + col) * rgbaType + 1] = SAFECOLOR(g);
	_image[(row * pixelsWide + col) * rgbaType + 2] = SAFECOLOR(b);
}

void GDImageFilterUtil::setRGBComponent(unsigned char* _image, unsigned int& pixelsWide, unsigned int& pixelsHigh, int row, int col, unsigned char r, unsigned char g, unsigned char b, int rgbaType, unsigned char a)
{
	_image[(row * pixelsWide + col) * rgbaType + 0] = SAFECOLOR(r);
	_image[(row * pixelsWide + col) * rgbaType + 1] = SAFECOLOR(g);
	_image[(row * pixelsWide + col) * rgbaType + 2] = SAFECOLOR(b);
	if (rgbaType == 4){ // 如果有透明值
		_image[(row * pixelsWide + col) * rgbaType + 3] = SAFECOLOR(a);
	}
}